'use strict';

const _ = require('underscore')
const Promise = require('bluebird')
const AV = require('leanengine')
const api = require('../wechat/wechatAPI')
const rp = require('request-promise')

const func = {
  getAll (req) {
    return rp(`https://birdie2.wilddogio.com/notification/${req.user.id}.json`).then(ret => Promise.resolve(JSON.parse(ret)))
  },

  send (req) {
    var userObjId = req.params.userObjId
    return rp({
      uri: `https://birdie2.wilddogio.com/notification/${userObjId}.json`,
      method: 'POST',
      body: JSON.stringify({
        type: 'doublesInvitation',
        inviterObjId: req.user.id
      })
    })
  }
}

AV.Cloud.define('notification', function (req, res) {
  var promise
  if (!req.user) {
    promise = Promise.reject({
      msg: 'not loged in'
    })
  } else {
    promise = func[req.params.method](req)
  }
  return promise.then(function (ret) {
    res.success(ret)
  }).catch(function (err) {
    console.log(err)
    res.error(err)
  })
})
